﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MachineBuildingSystem
{
    public partial class SqlCfgForm : XtraForm
    {
        public SqlCfgForm()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtServerIP.Text.Trim()))
            {
                MessageBox.Show("数据库IP地址不能为空！");
                txtServerIP.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txtDBName.Text.Trim()))
            {
                MessageBox.Show("数据库名称不能为空！");
                txtDBName.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txtUser.Text.Trim()))
            {
                MessageBox.Show("用户名不能为空！");
                txtUser.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txtPassword.Text.Trim()))
            {
                MessageBox.Show("密码不能为空！");
                txtPassword.Focus();
                return;
            }
            //修改配置文件的数据文件密码字段
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            string connectionStr = "Server=" + txtServerIP.Text.Trim()
                + ";Database=" + txtDBName.Text.Trim()
                + ";User ID=" + txtUser.Text.Trim()
                + ";Password=" + CommonHelper.Encrypt(txtPassword.Text.Trim(), "tp_ltscm")
                + ";Connection Timeout=10;Trusted_Connection=False";
            cfa.AppSettings.Settings["SQLConnectionStr"].Value = connectionStr;//设置config的值
            cfa.Save();
            ConfigurationManager.RefreshSection("appSettings");
            MessageBox.Show(this, "配置Sql数据库成功！");
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SqlCfgForm_Load(object sender, EventArgs e)
        {
            string connectionStr = ConfigurationManager.AppSettings["SQLConnectionStr"].ToString();
            string[] strArr = connectionStr.Split(';');
            foreach (string item in strArr)
            {
                if (item.StartsWith("Server"))
                    txtServerIP.Text = item.Substring("Server=".Length);
                if (item.StartsWith("Database"))
                    txtDBName.Text = item.Substring("Database=".Length);
                if (item.StartsWith("User ID"))
                    txtUser.Text = item.Substring("User ID=".Length);
                if (item.StartsWith("Password"))
                    txtPassword.Text = CommonHelper.Decrypt(item.Substring("Password=".Length), "tp_ltscm");
            }
        }
    }
}
